package com.tsinghuabigdata.edu.ddmath.module.neteaseim;

import com.netease.nimlib.sdk.AbortableFuture;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.AuthService;
import com.netease.nimlib.sdk.auth.LoginInfo;

/**
 * Created by 28205 on 2017/3/11.
 */
public class NeteaseLoginModel {
    private AbortableFuture<LoginInfo> loginRequest;

    public void loginNim(final String account, final String token, final NeteaseListener listener) {
        loginRequest = NIMClient.getService(AuthService.class).login(new LoginInfo(account, token));
        loginRequest.setCallback(new RequestCallback<LoginInfo>() {
            @Override
            public void onSuccess(LoginInfo param) {
                NimCache.setAccount(account);
                NeteaseAccountUtils.setNeteaseLoginInfo(param);

                // 初始化消息提醒
                // NIMClient.toggleNotification(UserPreferences.getNotificationToggle());

                // 初始化免打扰
                //NIMClient.updateStatusBarNotificationConfig(UserPreferences.getStatusConfig());

                // 进入主界面
                listener.onSuccess(param);
            }

            @Override
            public void onFailed(int code) {
                if (code == 302 || code == 404) {
//                    Toast.makeText(LoginActivity.this, R.string.login_failed, Toast.LENGTH_SHORT).show();
//                } else {
//                    Toast.makeText(LoginActivity.this, "登录失败: " + code, Toast.LENGTH_SHORT).show();
                }
                listener.onFailed(code);
            }

            @Override
            public void onException(Throwable exception) {
                listener.onException(exception);
            }
        });
    }

    public interface NeteaseListener {
        void onSuccess(Object res);

        void onFailed(int code);

        void onException(Throwable ex);
    }
}
